package com.yuke.cloud.service.omc.controller.frontend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.uac.dto.UserInfoDto;
import com.yuke.cloud.service.api.uac.service.UacUserFeignApi;
import com.yuke.cloud.service.api.wmc.dto.WmsGiftDto;
import com.yuke.cloud.service.omc.config.Quartz;
import com.yuke.cloud.service.omc.entity.OdsOrder;
import com.yuke.cloud.service.omc.entity.OdsPayOrder;
import com.yuke.cloud.service.omc.service.OdsPayOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by zys on 2018/12/10.
 */
@Api(value = "WEB - OdsPayOrderController", tags = "处理OdsPayOrder相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/ods/pay_order")
public class OdsPayOrderController extends BaseController {
    @Resource
    private OdsPayOrderService odsPayOrderService;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private UacUserFeignApi uacUserFeignApi;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "odsPayOrder", value = "该数据记录的POJO") @RequestBody OdsPayOrder odsPayOrder) {
        odsPayOrderService.save(odsPayOrder);
        return WrapMapper.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        odsPayOrderService.deleteById(id);
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "odsPayOrder", value = "需要修改的内容") @RequestBody OdsPayOrder odsPayOrder) {
        odsPayOrderService.update(odsPayOrder);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<OdsPayOrder> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        OdsPayOrder odsPayOrder = odsPayOrderService.findById(id);
        return WrapMapper.ok(odsPayOrder);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<OdsPayOrder>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                               @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<OdsPayOrder> list = odsPayOrderService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    /**
     * 用户订单修改
     * /ods/pay_order/updateOrderInfo
     *
     * @param odsOrder
     * @return
     */
    @PostMapping("/updateOrderInfo")
    @ApiOperation(httpMethod = "POST", value = "修改一条记录")
    public Wrapper updateOrderInfo(@ApiParam(name = "odsOrder", value = "需要修改的内容") @RequestBody OdsOrder odsOrder) {
        System.out.println("用户订单修改:" + odsOrder);
        if (odsOrder == null || odsOrder.getOrderId() == null || odsOrder.getGoodsList() == null || odsOrder.getGoodsList().size() < 1) {
            return WrapMapper.error("参数错误！");
        }
        odsPayOrderService.updateOrderInfo(odsOrder);
        return WrapMapper.ok();
    }

    /**
     * 获得订单结算后的信息，不提交
     * @param odsOrder
     * @return
     */
    @PostMapping("/getOrderInfo")
    @ApiOperation(httpMethod = "POST", value = "获得新订单重新计算后的真实数据")
    public Wrapper<OdsOrder> countOrderData(@ApiParam(name = "odsOrder", value = "需要修改的内容") @RequestBody OdsOrder odsOrder) {
        System.out.println("获得新订单重新计算后的真实数据,参数"+odsOrder);
        //todo 手机登录，取groupId从userGroup中，
        Long groupId = 4L;
        if (getGroupInfoDto() != null) {
            groupId = getGroupInfoDto().getGroupId();
        } else {
            Wrapper<UserInfoDto> userInfoDtoWrapper = uacUserFeignApi.getUserInfoByUserId(odsOrder.getUserId());
            if (userInfoDtoWrapper.getCode() == 200) {
                if (userInfoDtoWrapper.getResult().getGroupId() != null) {
                    groupId = userInfoDtoWrapper.getResult().getGroupId();
                }

            }
        }
        odsOrder.setGroupId(groupId);
        Wrapper<OdsOrder> wrapper = odsPayOrderService.getRealMoney(odsOrder);
        odsOrder = wrapper.getResult();

        if (odsOrder.getCouponReceiveId() != null && odsOrder.getCouponReceiveId() > 0) {

            Wrapper<OdsOrder> wrapperOrder = odsPayOrderService.doCouponCount(odsOrder);

            if (wrapperOrder.getCode() != 200) {
                return wrapperOrder;
            }
            odsOrder = wrapper.getResult();
        }

        //获得可用红包
        if (odsOrder.getRedReceiveId() != null && odsOrder.getRedReceiveId() > 0  && odsOrder.getRedMoney() != null) {
            Wrapper<OdsOrder> wrapperRed = odsPayOrderService.doRedCount(odsOrder);
            if (wrapperRed.getCode() != 200) {
                return wrapperRed;
            }
            odsOrder = wrapper.getResult();
        }

        return WrapMapper.ok(odsOrder);
    }



    @PostMapping("/getGiftSpecList")
    @ApiOperation(httpMethod = "POST", value = "获得赠品列表")
    public Wrapper<OdsOrder> getGiftSpecList(@ApiParam(name = "odsOrder", value = "需要修改的内容") @RequestBody OdsOrder odsOrder) {
        Wrapper<OdsOrder> wrapMapper = odsPayOrderService.getGiftSpecList(odsOrder);
        return WrapMapper.ok(wrapMapper.getResult());
    }

    @GetMapping("/addGiftSpec")
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper addGiftSpec(@ApiParam(name = "wmsGiftDto", value = "wmsGiftDto") @RequestBody WmsGiftDto wmsGiftDto) {

        String key = "Order:" + wmsGiftDto.getOrderId() + ":SpecId" + wmsGiftDto.getSpecId();
        redisTemplate.opsForValue().set(key, wmsGiftDto, Quartz.second, TimeUnit.SECONDS);
        return WrapMapper.ok();
    }

    /**
     * 结算修改后的订单，并提交数据，保存修改后的订单
     * @param odsOrder
     * @return
     */
    @PostMapping("/countMoneyAndUpdate")
    @ApiOperation(httpMethod = "POST", value = "获得新订单重新计算后的真实数据")
    public Wrapper<OdsOrder> countMoneyAndUpdate(@ApiParam(name = "odsOrder", value = "需要修改的内容") @RequestBody OdsOrder odsOrder) {
        Wrapper<OdsOrder> wrapper = this.countOrderData(odsOrder);
        odsOrder = wrapper.getResult();

        this.updateOrderInfo(odsOrder);
        return WrapMapper.ok(odsOrder);
    }




}